using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace DwarfFortClone
{

    public class DwarfGame : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        public Configuration configuration;
        Tilemap tileMap;
        public XNA2dCamera camera;

        public DwarfGame()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            configuration = new Configuration();
            tileMap = new Tilemap();
            List<List<uint>> tileArray = new List<List<uint>>();
            List<List<uint>> treeArray = new List<List<uint>>();
            Random generator = new Random();
            for (int r = 0; r < 100; r++)
            {
                tileArray.Add(new List<uint>());
                treeArray.Add(new List<uint>());
                for (int c = 0; c < 100; c++)
                {
                    tileArray[r].Add(1);
                    treeArray[r].Add(31);
                }
            }


            for (int i = 0; i < 100; i++)
            {
                int randomRow = generator.Next(0, 100);
                int randomColumn = generator.Next(0, 100);

                if (generator.Next(0, 3) == 0)
                {
                    treeArray[randomRow][randomColumn] = 19;
                }
                else if (generator.Next(0, 3) == 1)
                {
                    treeArray[randomRow][randomColumn] = 22;
                }
                else
                {
                    treeArray[randomRow][randomColumn] = 25;
                }
            }
            for (int i = 0; i < 100; i++)
            {
                int randomRow = generator.Next(0, 100);
                int randomColumn = generator.Next(0, 100);

                tileArray[randomRow][randomColumn] = 4;

                for (int walk = 0; walk < 10; walk++)
                {
                    randomRow += generator.Next(0, 3) - 1;
                    randomColumn += generator.Next(0, 3) - 1;

                    if (randomRow > 0 && randomRow < 100 && randomColumn > 0 && randomColumn < 100)
                    {
                        tileArray[randomRow][randomColumn] = 4;
                    }
                }
            }

            TileLayer lowerLayer = new TileLayer(Content, tileMap, "tileset", 16, tileArray);
            tileMap.AddLayer(lowerLayer);

            TileLayer treeLayer = new TileLayer(Content, tileMap, "tileset", 16, treeArray);
            tileMap.AddLayer(treeLayer);
        }

        protected override void Initialize()
        {
            configuration.Apply(this, graphics);
            camera = new XNA2dCamera(this);
            camera.Position = new Vector2(50, 50);
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            tileMap.LoadContent();
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            if (Keyboard.GetState().IsKeyDown(Keys.W))
            {
                camera.Position += new Vector2(0, -5);
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.S))
            {
                camera.Position += new Vector2(0, 5);
            }

            if (Keyboard.GetState().IsKeyDown(Keys.A))
            {
                camera.Position += new Vector2(-5, 0);
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.D))
            {
                camera.Position += new Vector2(5, 0);
            }
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(configuration.defaultBackgroundColor);
            spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.None, RasterizerState.CullCounterClockwise, null, camera.ViewTransformationMatrix());
            tileMap.Draw(spriteBatch);
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
